<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="编号">
              <a-input placeholder="" v-model="queryParam.clientId"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="密钥">
              <a-input placeholder="" v-model="queryParam.clientSecret"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button @click="() => {this.init()}" type="primary">查询</a-button>
            <a-button @click="() => queryParam = {}" style="margin-left: 8px">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button @click="$refs.clientAddOrUpdate.edit('','add')" icon="plus" type="primary">新建</a-button>
    </div>
    <a-table
      :columns="columns"
      :dataSource="tableData"
      :loading="loading"
      :pagination="pagination"
      :rowKey="record => record.userId"
      @change="handleTableChange"
    >
    </a-table>
    <client-add-or-update
      @ok="handleOk"
      ref="clientAddOrUpdate"
    ></client-add-or-update>
  </a-card>
</template>

<script>
import { fetchList, delObj } from '@api/sys/client'
import ClientAddOrUpdate from './clientAddOrUpdate'
export default {
  name: 'Client',
  components: {
    ClientAddOrUpdate
  },
  data () {
    return {
      loading: false,
      tableData: [],
      columns: [
        {
          title: 'id',
          customRender: (text, record, index) => {
            return (
              <div>
                {index + 1}
              </div>
            )
          }
        },
        {
          title: '编号',
          dataIndex: 'clientId'
        },
        {
          title: '密钥',
          dataIndex: 'clientSecret'
        },
        {
          title: '域',
          dataIndex: 'scope'
        },
        {
          title: '授权方式',
          dataIndex: 'authorizedGrantTypes'
        },
        {
          title: '令牌时效',
          dataIndex: 'accessTokenValidity'
        },
        {
          title: '刷新时效',
          dataIndex: 'refreshTokenValidity'
        },
        {
          title: '自动放行',
          dataIndex: 'autoapprove',
          customRender: (text, record, index) => {
            return (
              <div>
                {text ? '是' : '否'}
              </div>
            )
          }
        },
        {
          title: '操作',
          customRender: (text, record, index) => {
            return (
              <div>
                <a onClick={(e) => this.edit(record)}
                >修改</a>
                <a-divider type="vertical"/>
                <a-popconfirm title="是否删除客户端" onConfirm={(e) => this.delete(record)} okText="是" cancelText="否">
                  <a-icon slot="icon" type="question-circle-o" style="color: red"/>
                  <a href="javascript:;" style="color: red">删除</a>
                </a-popconfirm>
                <a-divider type="vertical"/>
                <a href="javascript:;"
                  onClick={(e) => this.show(record)}
                >查看</a>
              </div>
            )
          }
        }
      ],
      pagination: {
        pageSize: 10,
        current: 1,
        total: 0,
        showSizeChanger: true
      },
      pages: {
        size: 10,
        current: 1
      },
      queryParam: {
        clientId: '',
        clientSecret: ''
      }
    }
  },
  methods: {
    init () {
      this.loading = true
      fetchList({
        ...this.queryParam,
        ...this.pages
      }).then(res => {
        this.tableData = res.data.records
        this.pagination.total = res.data.total
        this.loading = false
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = pagination
      this.pages.size = pagination.pageSize
      this.pages.current = pagination.current
      this.init()
    },
    edit (record) {
      this.$refs.clientAddOrUpdate.edit(record.clientId, 'edit')
    },
    delete (record) {
      delObj(record.clientId).then(res => {
        this.$message.info('删除成功')
        this.init()
      })
    },
    show (record) {
      this.$refs.clientAddOrUpdate.edit(record.clientId, 'show')
    },
    handleOk () {
      this.init()
    }
  },
  created () {
    this.init()
  }
}
</script>

<style scoped>

</style>
